Hello everybody and welcome back to our visualization session with Matt Platt lip.
So in most cases the graph tells us more than a bunch of numbers.
And for the human eye a graph is typically more intuitive and understandable and therefore also Python
has an extensive library for viz. called met floodlit.
So that actually no limits on customizing grass that's met floodlit.
And yeah you could actually read em Zamir or read you any graph you might think often pacing and and
yeah it's just a matter of time because the functionality of met plot level is more or less unlimited.
So this session should serve as a short intro that enables you to make the most common plots like line
plots scatter plots and his programs and it should also enable you to make the most common and important
customizations to plots.
So first of all we have to impart the plotting framework of bad plot loop called Pi plot so we import
met plots that pi plot SPL T.
So this is a stand up convention to have the revelation P to you.
And if you work and tip in a notebook we should also have.
The magic function map plot lib in line.
So let's execute here.
And then first of all let's create a list with five elements 1 3 5 7 and 9
and we want to create a line plot of these values and therefore we're using the function period T dot
plot for the line plot and we pass our list to a peer to you dot plot and before we want to execute
and show our line plot we uh also showed you as pictured here dot so let's execute the cell here.
So now here we can see our line plot.
So when looking at the y axis we have one here.
Then we have uh three we have five we have seven.
And we have nine we could also change some the last element here too let's say six
so you in the end.
Then we have six.
And by default peer to dot plot starts with the Sera at the x axis.
So this is actually an interpolated line for the points 0 1
1 3 2 5
3 7 and 4 6 and we can also define x axis value so by default Python assumes integer starting from 0.
And here in this case we have 5 elements.
So from 0 to 5 excluding So this is the default case.
So we get here the same graph but we can redefine the x axis so let's say we want to have x values from
five to 10.
We can do this.
So now the Excel values are starting from five to nine.
So on if we cross up to show
then we can still see the plot here but also the FBI description of plot object and therefore it's best
practice here to have the P.A. so before we plot here
and we can also make a get up not to this.
The function P T dot scatter where we define x value.
So from 0 to 4 encoding and the y values is our list.
So let's see what we get here.
So we get your scatters and the first one has an x y you have zero and then y value of 1.
And the second one has an x value of 1 and the y value of 3 and the third one has a mixed value of two
and a y value of five.
And so on.
How why would we have to keep in mind that the amount of x values must match the length of our via values
here Sophia for example X extend range 7.
So with has seven elements of our sexes and only our list L with five elements.
Then we get an error message so X and Y must be the same size
all right.
So let's move on to a histogram and let's assume that we are standing downtown New York at a street
with a speed limit 50 miles per hour.
And we are measuring the speed of 10000 cars.
And for our purposes we assume that the speed is normally distributed was mean 55 and standard deviation
5.
So we are creating 10000 the normal distributed random numbers with mean fifty 55 and standard deviation
5.
So we are using here a list comprehension and we are assigning the driver speed in New York.
So actually the length of our list should be ten thousand.
Okay.
First of all we have to import the random module.
Then it should work and we can also check some elements here of our list speed New York.
So let's say let's check.
Uh the first 10 elements.
So we uh.
Fifty four fifty five point four and so on.
Forty eight.
And then we want to create a histogram With our 10000 measurements.
And therefore we are using purity hist and we are passing our list speak New York to beauty list.
And we are defining that we want to have 100 pins.
So let's see what we get here.
So here we have a histogram but our measurements so it's no surprise that s uh we created our measurements
with a normal distributed random numbers generator.
We get here kind of a battleship curve with um the mean at fifty five approximately.
And we can also change the amount of pins here we have 100 pins and uh let's see what happens if we
change it to 10.
So now we have only 10 Ben so that's maybe not the perfect solution here.
So maybe if we increase it to 1000 now this is maybe also not the best solution.
So one hundred bins for 10000 measurements would be a good solution here.
And now what we can do we can do many customizations.
So first of all so let's plot again.
Here we plot our histogram we are passing our list speed New York we are having 100 bins we are setting
the table to a New York data we will see later what this will bring us and also we set out a four to
1 and the color of our eyes again to read
let's draw the graph here and it's essentially the same graph as above.
Uh we only changed the color to red.
Yeah.
And then we can make your customization so first of all we can change in the style we use to see Bond
style.
So yeah there are many styles and Matt plotted and it's more or less a matter of taste but style you
want to use.
So if you change it to seaborne
then it looks a bit different a bit more scientific actually.
And we can also change the size of the figure by using the function Piers dot figure and set the fixed
size to let's say 10 6.
So now it's a bit larger.
We can also change here let's say 212 six and we can also add a title to our graph with some functional
purity job title and then we need to define the title as the strings here measure car speed speed limit
50 miles.
Power and what we can also do we can set the font size so the font size could be 15 year
so it's a bit larger than the excelsior and then we can also introduce labels for the Xs so we can introduce
um and label for the x axis with prototype X label and pass the string speed.
So now here we have speed
and uh the same.
We can do with the VI label and for example we can set up the label occurrences number of occurrences
here and we can also plot the vertical lines.
That's the function peer to peer v lines.
So and the purpose is to draw vertical line at the mean value of our collected our measured data.
So yeah let's see a shift tab.
What we need here.
So we need the x value.
And this is the mean value of our lists speed.
New York.
So we take the sum divided by the length and then we also have to define uh the minimum y value and
the maximum y value.
So here we can set it between 0 and 400 and then we can define that the colors of be red and the lines
should be here Darst and the labels would be a mean New York so let's see what we got here
right here you can see a we can see the test line but as our histogram is in transparent Yeah.
We cannot see our dashed line here and therefore we have to change and the out file for our histogram
so alpha equals 1 means there's no transparency of our histogram.
So if you change it to let's say 0 point 5 0 is a gram here is getting transparent and then we are seeing
the vertical line here.
Next we can set the range of taxes.
So here with POTUS access and let's see what we need here so we have to set X-Men X Max Wyman and Y
Max.
So why should be between let's say 0 and 400 and X should be the 3 and yeah 30 and maybe 90 so let's
run here.
And then we can also set the ticks off our axis.
So to say here we have ticks at the 30 40 50 and if you want to take set let's say 30 35 45 then we
have to change that here.
So from 30 to 91 we want to have ticks at every fifth value.
So here we are and the same we can do with them the VI label.
So it makes sense to have some ticks at every 50s.
No so we already have it here
and then we can define if you want to have the grid here or not.
And by default with them the Seabourn style it's enabled but we can also disable it with the force
let's make it for Reagan.
And before we head to our histogram we had the label the new york data.
And then our vertical line the label mean New York.
And to show the labels we have to call the function purity legend.
And in this beauty legend we can define the location of our legend.
So let's enable it here and let's press should tap
so he allocation string so it can pass best.
Or alternatively zero or upper right.
This one here I have center right center right is seven and so on and we also define the font size equals
13.
So let's see what we get here.
So now we have here our legend and we can also make many more plots within one graph.
So let's assume we are going to Boston and also measure 10000 times them the speed in a street with
the speed limit 50.
So let's assume the Boston people are driving faster and on average Uh 60 miles per hour and then 50
miles per hour zone.
And the standard deviation eight.
So let's create other random numbers and start in the list speed Boston.
And then we can also plot the histogram here from Boston.
So Patty Hearst we passed speed Boston.
We set pins to a 100.
Set the label to Boston data.
Alpha equals 0 point 5.
And the color to blue.
And let's see what happens.
Here we have both a histogram some New York and from Boston.
And one graph.
And it makes it very easy to compare and we can also enable here the vertical line at the mean of the
Boston data.
Yeah.
And here we can see on average I mean the Boston people are driving faster than the New York people.
And also with the variability of the Boston data is a bit higher.
So it's a bit more diverse here because of the standard deviation of our Boston data was higher with
eight.
All right.
We are finished now with our shot introduction to Matt Black live.
And if you want to go deeper into details I recommend you to go on the documentation side
so here you can find many examples and tutorials.
And if we have a look at the examples so you can see here the options are actually unlimited.
So there are hundreds or thousands of examples here
so have fun if you like and I hope you enjoyed the session and outtake take by.